﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;
namespace TestDxfDocument
{
    class ISYC_201_07_H7
    {
        public static void ISYC_201_07(Dictionary<string, Object> data) //凸三角形刀片
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour");//轮廓层
            Layer dimensioning = new Layer("dimensioning");//尺寸层
            Layer refin = new Layer("refin");//基准线/内轮廓/虚线
            Layer center = new Layer("center");//中心线
            Layer textlayer = new Layer("textlayer");//文本层
            Layer hatching = new Layer("hatching");//填充层
            Layer additionalline = new Layer("additionalline");//附加线
            Layer cutting = new Layer("cutting");//切削层
            fun.SetLayer(dxf);

            //基本参数-------------------------------------------------------------------------------------------------
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            //赋值参数--------------------------------------------------------------------------------------------------
            double IC = 12.7;
            double S = 4.76;
            double EPSR = 80;
            double RE = 1;
            double REN = 0.6;
            double L = 8.7;
            double AN = 7;
            double scale = IC / 12.7;//放大系数

            //点---------------------------------------------------------------------------------------------
            //第一部分
            //轮廓
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v1_1 = fun.Polar(v1, EPSR * pi / 180, L / 2.0);
            Vector2 v2 = fun.Polar(v1, EPSR * pi / 180, L);
            Vector2 v6 = fun.Polar(v1, 0 * pi / 180, L);
            Vector2 v7 = fun.Inters(v1, fun.Polar(v1, EPSR / 2.0 * pi / 180, L * 2), v6, fun.Polar(v6, (EPSR / 2.0 + 60) * pi / 180, L * 2));
            Vector2 v3 = fun.rotatePoint_Changed(v7, v1, -120);
            Vector2 v4 = fun.rotatePoint_Changed(v7, v2, -120);
            Vector2 v5 = fun.rotatePoint_Changed(v7, v3, -120);
            Vector2 v8 = fun.Polar(v6, 0 * pi / 180, L * 4);
            Vector2 v9 = fun.Polar(v8, (180 - AN) * pi / 180, S / Math.Cos(AN * pi / 180));
            Vector2 v11 = new Vector2(v8.X, v8.Y + Math.Abs(v3.Y - v1.Y));
            Vector2 v10 = fun.Inters(v9, fun.Polar(v9, 90 * pi / 180, L), v11, fun.Polar(v11, -(180 - AN) * pi / 180, L));
            Vector2 v12 = new Vector2(v8.X, v7.Y);

            //作图部分-----------------------------------------------------------------------
            //第一部分
            //轮廓
            LwPolyline polyline1 = new LwPolyline();
            polyline1.Vertexes.Add(new LwPolylineVertex(v1_1));
            polyline1.Vertexes.Add(new LwPolylineVertex(v2));
            polyline1.Vertexes.Add(new LwPolylineVertex(v3));
            polyline1.Vertexes.Add(new LwPolylineVertex(v4));
            polyline1.Vertexes.Add(new LwPolylineVertex(v5));
            polyline1.Vertexes.Add(new LwPolylineVertex(v6));
            polyline1.Vertexes.Add(new LwPolylineVertex(v1));
            polyline1.Vertexes.Add(new LwPolylineVertex(v1_1));
            fun.PolyFill(dxf, polyline1, 0.05, contour);

            //倒角
            var arc1 = Pus.Fillet4LwPline(polyline1, 1, REN);
            var arc2 = Pus.Fillet4LwPline(arc1, 3, RE);
            var arc3 = Pus.Fillet4LwPline(arc2, 5, REN);
            var arc4 = Pus.Fillet4LwPline(arc3, 7, RE);
            var arc5 = Pus.Fillet4LwPline(arc4, 9, REN);
            var arc6 = Pus.Fillet4LwPline(arc5, 11, RE);
            fun.PolyFill(dxf, arc6, 0.3, contour);

            //第二部分
            //轮廓
            LwPolyline polyline2 = new LwPolyline();
            polyline2.Vertexes.Add(new LwPolylineVertex(v8));
            polyline2.Vertexes.Add(new LwPolylineVertex(v9));
            polyline2.Vertexes.Add(new LwPolylineVertex(v10));
            polyline2.Vertexes.Add(new LwPolylineVertex(v11));
            polyline2.Vertexes.Add(new LwPolylineVertex(v12));
            polyline2.Vertexes.Add(new LwPolylineVertex(v8));
            fun.PolyFill(dxf, polyline2, 0.3, contour);

            //虚线
            Circle circle1 = new Circle(v7, IC / 2.0);
            circle1.Layer = refin;
            circle1.LinetypeScale = 5;
            circle1.Linetype = Linetype.Dashed;
            dxf.AddEntity(circle1);

            //标注---------------------------------------------------------------------------
            double height = 1 * scale;
            double arrowsize = 0.5 * scale;
            double exline = 0.5 * scale;

            //坐标系
            fun.Axis(dxf, v7, 90, 0, "Y", "X", "MCS", 0.15 * scale, height * 0.6, 0);
            fun.Axis(dxf, v1, 90, 0, "Y", "X", "PCS", 0.15 * scale, height * 0.6, 0);
            fun.Axis(dxf, v12, 90, 0, "Y", "Z", "MCS", 0.15 * scale, height * 0.6, 0);
            fun.Axis(dxf, v8, 90, 0, "Y", "Z", "PCS", 0.15 * scale, height * 0.6, 0);

            //L
            fun.dimrot1(dxf, v1, v6, 180, L * 0.5, 0.1, height, arrowsize, exline, "L", "", "");

            //S
            fun.dimrot1(dxf, v8, v9, 180, L * 0.5 + (Math.Abs(v9.Y - v8.Y) / 2.0), 0.1, height, arrowsize, exline, "S", "", "");

            //角度标注
            //AN
            fun.jdbz2(dxf, new Line(v9, fun.Polar(v9, (180 - AN) * pi / 180, 0.1)), new Line(v8, fun.Polar(v8, 180 * pi / 180, 0.1)), S * 2, height, arrowsize, exline, "AN", "", "");

            //EPSR
            fun.jdbz2(dxf, new Line(v3, fun.Polar(v3, fun.CalAngle(v2, v3), 0.1)), new Line(v3, fun.Polar(v3, fun.CalAngle(v4, v3), 0.1)), S, height, arrowsize, exline, "EPSR", "", "");

            //RE
            Vector2 re = fun.Polar(v7, fun.CalAngle(v7, v5), fun.CalDistance(v7, v5) - RE / Math.Sin(EPSR / 2.0 * pi / 180) + RE);
            fun.jt(dxf, re, -30, S, 1, "RE");

            //REN
            Vector2 ren = fun.Polar(v7, fun.CalAngle(v7, v4), fun.CalDistance(v7, v4) - RE / Math.Sin((180 - 60 - EPSR / 2.0) * pi / 180) + RE);
            fun.jt(dxf, ren, 45, S, 1, "REN");
            fun.SetLayer(dxf);
            fun.SetByLayer(dxf);

            //定义格式，根据路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}
